package cn.jiahui;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class LiftWriteThread extends Thread {
	Socket socket;
	String hostAddress = "";
	BufferedOutputStream bos = null;
	int synInterval = 1000;

	final int CMD_TYPE_READ_BTN_STATE = 1;
	final int CMD_TYPE_READ_LIFT_STATE = 2;
	final int CMD_TYPE_SET_BTN = 3;

	byte[] btnState = new byte[] { (byte) 0XA1, (byte) 0XA2, (byte) 0X00, (byte) 0X00, (byte) 0X00, (byte) 0X06,
			(byte) 0X01, (byte) 0X01, (byte) 0X00, (byte) 0X01, (byte) 0X00, (byte) 0X0A };
	byte[] liftState = new byte[] { (byte) 0xB1, (byte) 0xB2, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06,
			(byte) 0x01, (byte) 0x03, (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x02 };

	byte[] data = new byte[] { (byte) 0x03, (byte) 0x6E, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06,
			(byte) 0x01, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff };

	public LiftWriteThread(Socket s, LiftService liftService,int synInterval) {
		this.socket = s;
		hostAddress = socket.getInetAddress().getHostAddress();
		this.synInterval = synInterval;
		OutputStream outputStream = null;
		try {
			outputStream = socket.getOutputStream();
			bos = new BufferedOutputStream(outputStream);

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void run() {
		int i = CMD_TYPE_READ_BTN_STATE;
		try {
			while (true) {
				Thread.sleep(synInterval);
				writeStateCommond(CMD_TYPE_READ_BTN_STATE);
				writeStateCommond(CMD_TYPE_READ_LIFT_STATE);
				
//				if (i == CMD_TYPE_READ_BTN_STATE) {
//					writeStateCommond(CMD_TYPE_READ_BTN_STATE);
//					i = CMD_TYPE_READ_LIFT_STATE;
//				} else if (i == CMD_TYPE_READ_LIFT_STATE) {
//					writeStateCommond(CMD_TYPE_READ_LIFT_STATE);
//					i = CMD_TYPE_READ_BTN_STATE;
//				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void writeStateCommond(int commondType) throws IOException {
		System.out.println("writeLiftCommond() " + commondType);
		if (commondType == CMD_TYPE_READ_BTN_STATE) {
			writeCommondForLiftBtnState();
		}
		if (commondType == CMD_TYPE_READ_LIFT_STATE) {
			writeCommondForLiftState();
		}

	}

	private void writeCommondForLiftBtnState() throws IOException {
		// TODO 这些数据应该是从电梯那边读取来的.

		// 可以用上面的代码做下校验,看btnState是否正常.
		bos.write(btnState);
		bos.flush();
		System.out.println("sendCommondReadBtnState()");
	}

	private void writeCommondForLiftState() throws IOException {
		bos.write(liftState);
		bos.flush();
		System.out.println("sendCommonForReadLiftState()");
	}

	public boolean writeCommondForSetBtnState(int btnCh, int light) throws IOException {

//		if (btnCh == 1) {
//			data[9] = 0x01;
//		}
//		if (btnCh ==  2) {
//			data[9] = 0x02;
//		}
//		if (btnCh == 3) {
//			data[9] = 0x03;
//		}
//		if (btnCh == 4) {
//			data[9] = 0x04;
//		}
//		if (btnCh == 5) {
//			data[9] = 0x05;
//		}
//		if (btnCh == 6) {
//			data[9] = 0x06;
//		}
//		if (btnCh == 7) {
//			data[9] = 0x07;
//		}
//		if (btnCh == 8) {
//			data[9] = 0x08;
//		}
//		if (btnCh == 9) {
//			data[9] = 0x09;
//		}
		byte btnId = (byte)btnCh;
		
		if(btnId > 0x0A || btnCh < 0x01){
			
			return false;
		}
		data[9] = (byte)btnCh;
		bos.write(data);
		bos.flush();
		System.out.println("sendCommonForSetBtnState()");

		return true;
	}

}
